
import {reqGetshopCart,reqDeleteCartbyId,reqCheckedbyId} from '../../api/index'

const state={ 
      shopcartList:[]
}
const actions={
   //获取购物车列表
   async getshopCartList(context){
      let result=await reqGetshopCart()
         if(result.code===200){
          context.commit('GETSHOPCARTLIST',result.data)
         }else{
         console.log('失败')
         }
   },
   //删除购物车某个产品
   async deleteCartbyId(context,skuID){
      let result=await reqDeleteCartbyId(skuID)
      console.log(result)
         if(result.code===200){
         return 'ok'
         }else{
         return Promise.reject(new Error("删除产品失败"))
         }
   },
    //切换产品选定状态
    async CheckedbyId(context,{skuID,isChecked}){
      let result=await reqCheckedbyId(skuID,isChecked)
         if(result.code===200){
         return 'ok'
         }else{
         return Promise.reject(new Error("修改产品状态失败"))
         }
   },
   //改变购物车全选
   async updateAllchecked(context,isChecked){
      let promiseALL=[];
      context.getters.cartList.cartInfoList.forEach(item => {
         let promise=context.dispatch('CheckedbyId',{skuID:item.skuId,isChecked:isChecked})
        promiseALL.push(promise)
      });
       return Promise.all(promiseALL);
   },
}

 
const mutations={
   GETSHOPCARTLIST(state,value){
      state.shopcartList=value
   }
}
const getters={
   cartList(state){
      return state.shopcartList[0]||[]
   }
   }
export default{
   state,actions,mutations,getters
}
